<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>DOM操作CSS</title>
    <style>
        #d1{
            width: 200px;
            height: 200px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div id="d1"></div>
    <input type="button" id="changeBtn" value="change">
    <div id="d2">
        <p id="p1">
            <span id="s1">文本</span>
        </p>
    </div>
    <input type="text" id="ipt">
    <script>
        let d1 = document.getElementById("d1");
        let changeBtn = document.getElementById("changeBtn");
        changeBtn.onclick = function(){
            d1.style.width = "100px";
            d1.style.height = "100px";
            d1.style.backgroundColor = "yellow";
        }

        let d2 = document.getElementById("d2");
        let p1 = document.getElementById("p1");
        let s1 = document.getElementById("s1");
        s1.onclick = function(event){
            console.log("s1",event);
            // 阻止事件冒泡
            event.stopPropagation();
        }
        p1.onclick = function(){
            console.log("p1");
        }
        d2.onclick = function(){
            console.log("d2");
        }
        document.body.onclick = function(){
            console.log("body");
        }

        s1.addEventListener("click",function(){
            console.log("捕获s1");
        },true);
        p1.addEventListener("click",function(){
            console.log("捕获p1");
        },true);
        d2.addEventListener("click",function(){
            console.log("捕获d2");
        },true);
        document.body.addEventListener("click",function(){
            console.log("捕获body");
        },true);

        document.getElementById("ipt").onkeydown = function(event){
            console.log(event.keyCode);
            if(event.keyCode < 48 || event.keyCode>57){
                event.preventDefault();
            }
            
        }
    </script>
</body>
</html>